<?php

namespace app\stores\model\cash;

use app\beautiful\model\mall\GoodsModel;
use app\common\model\CommonModel;


class PrintRuleModel extends CommonModel{

    protected  $name = 'beautiful_print_rule';

    protected  $pk   = 'print_rule_id';
    public static $default_receipt=[
        1=>[1,2,5,20,21,22],
        2=>[10,11,12,13,14,17,23],
        3=>[11,12,15],
    ];
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("edit_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function info($where,$field='*')
    {
        $re = self::where($where)->field($field)->find();
        if(empty($re)){
            if(isset($where['type'])){
                $data['shop_id'] = $where['shop_id'];
                $data['type'] = $where['type'];
                $data['add_ip'] = request()->ip();
                $data['default_receipt_ids'] = implode(',',self::$default_receipt[$where['type']]);

                self::insertGetId($data);

                $re = self::where($where)->field($field)->find();
            }
        }
        $res_s=[];
        $res_s[]=['id','IN',self::$default_receipt[$where['type']]];
        $res_s[]=['is_effect','=',1];
        $re['default_receipt_ids'] =ReceiptModel::where($res_s)->select()->toArray();
        $re['receipt_ids'] =ReceiptModel::whereIn('id',explode(',',$re['receipt_ids']))->select()->toArray();
        $g_s=[];
        $g_s[]=['goods_id','IN',explode(',',$re['not_print_goods_ids'])];
        $g_s[]=['origin_type','=',2];
        $g_s[]=['is_delete','=',0];
        $re['not_print_goods_ids'] = GoodsModel::where($g_s)->field('goods_id,name')->select()->toArray();
        return $re;
    }
    public static function list($where,$order,$page=0,$limit=0,$field='*'){
        $query =  self::alias('ts')
            ->leftjoin('account a','a.account_id=ts.account_id')
            ->where(['ts.is_delete'=>0,'a.is_delete'=>0])
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['ts.status'=>$where['status']]);
            })
            ->when(isset($where['search']),function ($query) use ($where){
                $query->where('ts.settlement_name','like','%'.$where['search'].'%');
            })
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['ts.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['settlement_type']),function ($query) use ($where){
                $query->where(['ts.settlement_type'=>$where['settlement_type']]);
            })->field($field);
        $count = $query->count();
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->order($order)->select()->toArray();

        return compact('count','list');
    }

}

